package model.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import Hibernate.dao.MobileAdministrator;
import Hibernate.dao.MobileAdministratorDAO;

public class AdminService {
	Map<String, String> map;
	MobileAdministratorDAO mobileAdministratorDao;

	public void setMobileAdministratorDao(
			MobileAdministratorDAO mobileAdministratorDao) {
		this.mobileAdministratorDao = mobileAdministratorDao;
	}

	public String login(String adminInfo) {
		map = new HashMap<String, String>();
		try {
			JSONObject jadminInfo = JSONObject.fromObject(adminInfo);
			System.out.print(adminInfo);
			if (jadminInfo.containsKey("adminId")
					&& jadminInfo.containsKey("adminPsd")) {
				MobileAdministrator tempAdmin = mobileAdministratorDao
						.findById(jadminInfo.getString("adminId"));
				if (jadminInfo.getString("adminPsd").equals(
						tempAdmin.getAdminPsd())) {
					map.put("flag", "true");
				} else {
					map.put("flag", "false");
					map.put("reason", "用户名或密码错误");
				}
			} else {
				map.put("flag", "false");
				map.put("reason", "用户或密码名为空");
			}
		} catch (JSONException e) {
			map.put("flag", "false");
			map.put("reason", "数据格式不正确");
		}
		return JSONObject.fromObject(map).toString();
	}
	
	public String ExcelUpload(File file,String FileName)
	{
		map = new HashMap<String,String>();
		System.out.println(FileName);
			if(FileName==null||!FileName.endsWith(".xls"))
			{
				map.put("flag", "false");
				map.put("Info", "文件名为空 or 文件类型错误");
			}
			else if(file==null||!file.exists())
			{
				map.put("flag", "false");
				map.put("Info", "此文件不存在！");
			}else
			{
				return ExcelToAdmin(file);
			}
			return JSONObject.fromObject(map).toString();
	}
	
	public String ExcelToAdmin(File file) {
		map = new HashMap<String, String>();
		Map<String, String> value = new HashMap<String, String>();
		ArrayList<String> list = new ArrayList<String>();
		try {
			// 创建对Excel工作簿文件的引用
			HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(file));
			// 在Excel文档中，第一张工作表的缺省索引是0
			// 其语句为：HSSFSheet sheet = workbook.getSheetAt(0);
			HSSFSheet sheet = wookbook.getSheet("mAdmins");
			// 获取到Excel文件中的所有行数
			int rows = sheet.getPhysicalNumberOfRows();
			// 遍历行
			for (int i = 0; i < rows; i++) {
				// 读取左上端单元格
				HSSFRow row = sheet.getRow(i);
				// 行不为空
				if (row != null) {
					// 获取到Excel文件中的所有的列
					int cells = row.getPhysicalNumberOfCells();
					// StringBuilder value = new StringBuilder();
					// StringBuilder info = new StringBuilder();
					// 遍历列
					for (int j = 0; j < cells; j++) {
						// 获取到列的值
						HSSFCell cell = row.getCell(j);
						if (cell != null) {
							switch (cell.getCellType()) {
							case HSSFCell.CELL_TYPE_FORMULA:
								break;
							case HSSFCell.CELL_TYPE_NUMERIC:
								if (i == 0)
									list.add(Double.toString(cell
											.getNumericCellValue()));
								else
									value.put(list.get(j), Double.toString(cell
											.getNumericCellValue()));
								break;
							case HSSFCell.CELL_TYPE_STRING:
								if (i == 0)
									list.add(cell.getStringCellValue().trim());
								else
									value.put(list.get(j), cell
											.getStringCellValue().trim());
								break;
							default:
								value.put(list.get(j), "");
								break;
							}
						}
					}
					if (i != 0) {
						for (int n = cells; n < list.size(); n++) {
							value.put(list.get(n), "");
						}
						MobileAdministrator admin = new MobileAdministrator();
						admin.setInfo(value);
						mobileAdministratorDao.attachDirty(admin);
					}
				}
			}
			wookbook.close();
			map.put("flag", "true");
			map.put("Info", "success");
		} catch (FileNotFoundException e) {
			map.put("flag", "false");
			map.put("Info", e.getMessage());
			// e.printStackTrace();
		} catch (IOException e) {
			map.put("flag", "false");
			map.put("Info", e.getMessage());
			// e.printStackTrace();
		}
		return JSONObject.fromObject(map).toString();
	}

	public String adminAll() {
		// TODO Auto-generated method stub
		map = new HashMap<String,String>();
		JSONArray adminJson = new JSONArray();
		List list = mobileAdministratorDao.findAll();
		System.out.println(list);
		for(int i=0;i<list.size();i++){
			MobileAdministrator admin = (MobileAdministrator) list.get(i);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("adminId",admin.getAdminId());
			jsonObject.put("epname",admin.getEpname());
			adminJson.add(jsonObject);
		}
		map.put("flag","true");
		map.put("data", adminJson.toString());

		return JSONObject.fromObject(map).toString();
	}
}
